//
//  DUNormalTableViewCell.m
//  toutiao
//
//  Created by duyujin1 on 2021/7/22.
//

#import "DUNormalTableViewCell.h"
#import "DUListItem.h"
#import "SDWebImage.h"
#import "Masonry.h"

@interface DUNormalTableViewCell()

@property(nonatomic,strong,readwrite) UILabel * titleLable;
@property(nonatomic,strong,readwrite) UILabel * sourceLable;
@property(nonatomic,strong,readwrite) UILabel * timeLbl;
@property(nonatomic,strong,readwrite) UILabel * commentLbl;

@property(nonatomic,strong,readwrite) UIImageView * rightImageView;
@property(nonatomic,strong,readwrite) UIButton * deleteButton;
@property(nonatomic,strong,readwrite) UIView * line;

@end

@implementation DUNormalTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{
    if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        //cell状态可编辑的时候采用self.contentView
        [self.contentView addSubview:self.titleLable];
        [self.contentView addSubview:self.sourceLable];
        [self.contentView addSubview:self.timeLbl];
        [self.contentView addSubview:self.commentLbl];
        [self.contentView addSubview:self.rightImageView];
        [self.contentView addSubview:self.deleteButton];
        [self.contentView addSubview:self.line];
        [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).offset(15);
            make.left.mas_equalTo(self).offset(15);
            make.right.mas_equalTo(self.deleteButton);
        }];
        [self.sourceLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.rightImageView);
            make.left.mas_equalTo(self.titleLable);
        }];
        [self.timeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.sourceLable);
            make.left.mas_equalTo(self.sourceLable.mas_right).offset(10);
        }];
        [self.commentLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.timeLbl);
            make.left.equalTo(self.timeLbl.mas_right).offset(10);
            make.right.equalTo(self.deleteButton.mas_left).offset(-10).priorityHigh();//优先级高，必须保证到右边的距离
        }];
        [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.rightImageView);
            make.right.mas_equalTo(self.rightImageView.mas_left).offset(-10);
            make.height.mas_equalTo(15);
            make.width.mas_equalTo(20);
        }];
        [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).offset(10);
            make.bottom.mas_equalTo(self).offset(-10);
            make.right.mas_equalTo(self).offset(-15);
            make.width.mas_equalTo(100);
        }];
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLable);
            make.bottom.mas_equalTo(self);
            make.right.mas_equalTo(self.rightImageView);
            make.height.mas_equalTo(1);
        }];
    }
    return self;
}

-(UILabel *)titleLable{
    if(!_titleLable){
        _titleLable=[[UILabel alloc] initWithFrame:CGRectZero];
        _titleLable.font=[UIFont systemFontOfSize:16];
        _titleLable.textColor=[UIColor blackColor];
        _titleLable.numberOfLines=2;
        _titleLable.lineBreakMode=NSLineBreakByTruncatingTail;
    }
    return _titleLable;
}
-(UILabel *)sourceLable{
    if(!_sourceLable){
        _sourceLable=[[UILabel alloc] initWithFrame:CGRectZero];
        _sourceLable.font=[UIFont systemFontOfSize:12];
        _sourceLable.textColor=[UIColor grayColor];
        //位置不够挤一挤
        [_sourceLable setContentCompressionResistancePriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _sourceLable;
}
-(UILabel *)timeLbl{
    if(!_timeLbl){
        _timeLbl=[[UILabel alloc] initWithFrame:CGRectZero];
        _timeLbl.font=[UIFont systemFontOfSize:12];
        _timeLbl.textColor=[UIColor grayColor];
        //完整显示
        //实在实在不行也可以挤一挤，谁让commentLbl后进来呢
        //[_timeLbl setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _timeLbl;
}
-(UILabel *)commentLbl{
    if(!_commentLbl){
        _commentLbl=[[UILabel alloc] initWithFrame:CGRectZero];
        _commentLbl.font=[UIFont systemFontOfSize:12];
        _commentLbl.textColor=[UIColor grayColor];
        //完整显示
        [_commentLbl setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _commentLbl;
}
-(UIImageView *)rightImageView{
    if(!_rightImageView){
        _rightImageView=[[UIImageView alloc] initWithFrame:CGRectZero];
        _rightImageView.contentMode=UIViewContentModeScaleAspectFill;
        _rightImageView.layer.masksToBounds=YES;
    }
    return _rightImageView;
}
-(UIButton *)deleteButton{
    if(!_deleteButton){
        _deleteButton=[[UIButton alloc] initWithFrame:CGRectZero];
        [_deleteButton setTitle:@"X" forState:UIControlStateNormal];
        [_deleteButton setTitle:@"V" forState:UIControlStateHighlighted];
        [_deleteButton setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
        [_deleteButton setTitleColor:UIColor.lightGrayColor forState:UIControlStateHighlighted];
        _deleteButton.titleLabel.font=[UIFont systemFontOfSize:12];
        _deleteButton.layer.cornerRadius=7;
        _deleteButton.layer.masksToBounds=YES;
        _deleteButton.layer.borderWidth=1;
        _deleteButton.layer.borderColor=[UIColor lightGrayColor].CGColor;
        [_deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];;
    }
    return _deleteButton;
}

-(UIView *)line{
    if(!_line){
        _line=[[UIView alloc]initWithFrame:CGRectZero];
        _line.backgroundColor=[UIColor blackColor];
        _line.alpha=0.1;
    }
    return _line;
}

-(void)layoutTableViewCellWithItem:(DUListItem *) item{
//    BOOL hasRead=[[NSUserDefaults standardUserDefaults] boolForKey:item.sourceId];
//    if(hasRead){
//        self.titleLable.textColor=[UIColor lightGrayColor];
//    }else{
//        self.titleLable.textColor=[UIColor blackColor];
//    }
    self.titleLable.text=item.title;
    self.sourceLable.text=item.source;
    self.timeLbl.text=item.mtime;
    self.commentLbl.text=[NSString stringWithFormat:@"%@评论",item.replyCount];

    
//    //GCD
//    //创建一个非主线程
//    dispatch_queue_global_t downLoadQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_queue_main_t mainQueue=dispatch_get_main_queue();
//
//    dispatch_async(downLoadQueue, ^{
//        UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:item.picUrl]]];
//        dispatch_async(mainQueue, ^{
//            self.rightImageView.image=image;
//        });
//    });
    
    //[self.rightImageView sd_setImageWithURL:item.picUrl];
    [self.rightImageView sd_setImageWithURL:(NSURL*)item.imgsrc completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            NSLog(@"");
    }];
}

-(void)deleteButtonClick{
    if(self.delegate && [self.delegate respondsToSelector:@selector(tableViewCell: clickDeleteButton:)]){
        [self.delegate tableViewCell:self clickDeleteButton:self.deleteButton];
    }
}

@end
